package com.lmabbe.core.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lmabbe.common.global.result.R;
import com.lmabbe.core.common.converter.SysOrgConverter;
import com.lmabbe.core.common.dto.SysOrgDTO;
import com.lmabbe.core.common.entitys.SysOrg;
import com.lmabbe.core.common.interfaces.SysOrgService;
import com.lmabbe.core.common.query.SysOrgQuery;
import com.lmabbe.core.common.vo.SysOrgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 部门表管理
 *
 * @author lmabbe
 */
@Api(tags = "部门表管理")
@RestController
@RequestMapping("sysOrg")
@RequiredArgsConstructor
public class SysOrgController {

    @DubboReference
    private SysOrgService sysOrgService;
    private final SysOrgConverter sysOrgConverter;

    /**
     * 获取所有
     *
     * @return
     */
    @Operation(summary = "获取所有")
    @GetMapping("all")
    public R<List<SysOrgVO>> list() {
        List<SysOrg> list = sysOrgService.list();
        return R.success(sysOrgConverter.list2Vos(list));
    }

    /**
     * 分页获取
     *
     * @param query
     * @return
     */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<SysOrgVO>> list(SysOrgQuery query) {
        IPage page = sysOrgService.selectPage(query);
        page.setRecords(sysOrgConverter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
     * 获取单个实体类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<SysOrgVO> getById(@PathVariable Long id) {
        return R.success(sysOrgConverter.entity2Vo(sysOrgService.getById(id)));
    }

    /**
     * 新增单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody SysOrgDTO dto) {
        this.sysOrgService.save(sysOrgConverter.dto2Entity(dto));
        return R.success();
    }

    /**
     * 修改单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改单个实体类")
    @PutMapping("{orgId}")
    public R update(@RequestBody SysOrgDTO dto, @PathVariable Long orgId) {
        this.sysOrgService.updateById(sysOrgConverter.dto2Entity(dto, orgId));
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.sysOrgService.removeById(id);
        return R.success();
    }

    /**
     *
     * @param parentId :
     * @return {@link R < List< SysOrg>>}
     * @date 2021/9/11 5:17 下午
     * @author liming
     */
    @Operation(summary = "根据上级ID获取部门列表")
    @GetMapping("listByParentId")
    @ApiImplicitParam(name = "parentId", value = "上级部门ID")
    public R<List<SysOrgVO>> getOrgByParentId(String parentId) {
        return R.success(sysOrgConverter.list2Vos(sysOrgService.getOrgListByParentId(parentId)));
    }


    /**
     * 获取当前部门所有上级
     *
     * @param orgId 部门ID
     * @return {@link R < List< SysOrg>>}
     * @date 2021/9/11 5:17 下午
     * @author liming
     */
    @Operation(summary = "根据上级ID获取部门列表")
    @GetMapping("{orgId}/allParent")
    @ApiImplicitParam(name = "orgId", value = "当前部门ID")
    public R<List<SysOrgVO>> getOrgAllParent(@PathVariable String orgId) {
        return R.success(sysOrgConverter.list2Vos(sysOrgService.getOrgAllParent(orgId)));
    }


}